package mydic;

import java.io.*;

public class Dicdaoio implements DicDao {

  @Override
  public void save(Mydic mydic) {
//吧传入的参数mydic对象写入文件d：\\txt
//        字符缓冲流一次写一行
    BufferedWriter bw = null;
    try {
//            追加内容\

      bw = new BufferedWriter(new FileWriter("D:\\d.txt", true));
      bw.write(mydic.getId() + "_" + mydic.getEnglish() + "_" + mydic.getChinese() + "_" + mydic
          .getBeizhu());
      bw.newLine();
      bw.flush();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (bw != null) {
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

  }




  @Override
  public Mydic query(String name) {
/*
根据传入的英文name  返回词典对象
循环读取文件中的每一个温恩
比较name何文本
相同：返回对象
 */
BufferedReader br=null;
        try {
            br=new BufferedReader(new FileReader("d:\\d.txt"));
            String line=null;
            while ((line=br.readLine())!=null)
            {
             String[] data=   line.split("_");
             if(name.equals(data[1]))
             {
                 int id=Integer.parseInt(data[0]);
                 String english=data[1];
                 String chinese=data[2];
                 String beizhu=data[3];
                 Mydic d=new Mydic(english,chinese,beizhu,id);
                 return  d;
             }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    public void queryall() {

        BufferedReader br=null;
        try {
            br=new BufferedReader(new FileReader("d:\\d.txt"));
            String line=null;
            while ((line=br.readLine())!=null)
            {
                System.out.println(line);
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(br!=null)
            {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void delete(String name) {
//        x新建一个文件txt    循环读取源文件的每一行文本
// 比价这行文本是否需要删除
//        如果不是，写入新文件
//删除源文件
//新文件重命名
if(query(name)==null)
{
  System.out.println("删除的内容不存在");
  return;
}
else
      {
        BufferedWriter bw=null;
        BufferedReader br=null;

        try {
          //字符缓冲输入，读取源文件的内容
          bw=new BufferedWriter(new FileWriter("d:\\new.txt",true)) ;
          //字符缓冲输出，写入新文件
          br=new BufferedReader(new FileReader("d:\\d.txt"));
String line;
        //循环读取
          while ((line=br.readLine())!=null)
          {
            String [] data=line.split("_");
            if(!name.equals(data[1]))
            {
              bw.write(line);
              bw.newLine();
              bw.flush();
            }
          }

        } catch (IOException e) {
          e.printStackTrace();
        }
   finally {
          if(br!=null)
          {
            try {
              br.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          if(bw!=null)
          {
            try {
              bw.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        }

       File oldfile=new File("d:\\d.txt") ;
        File newfile=new File("d:\\new.txt");





        //if(oldfile.delete())
        //{
        //  if(newfile.renameTo(oldfile))
        //  {
        //    System.out.println();
        //  }
        //}
        //



      }
    }
}
